;; Tests of the longest common subsequence functions
;; Copyright (C) 2022  Rocks Mazama
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

(ns mazama.sequence.lcs-test
  (:require
   [clojure.test :refer [deftest is testing]]
   [mazama.sequence.lcs :refer [lcs]]))

(defn- stringify [s]
  (set (map #(apply str %) s)))

(deftest small-test
  (testing "nil nil"
    (is (empty? (lcs nil nil)))
    (is (empty? (lcs () ()))))
  (testing "nil _"
    (is (empty? (lcs nil "something")))
    (is (empty? (lcs () "other"))))
  (testing "_ nil"
    (is (empty? (lcs [:a :b :c :d] nil)))
    (is (empty? (lcs (list 2 3 5 7) ())))))

(deftest wikipedia-test
  (testing "https://en.wikipedia.org/wiki/Longest_common_subsequence_problem"
    (is (= #{"ABD" "ACD"} (stringify (lcs "ABCD" "ACBAD")))))
  (testing "Worked example"
    (is (= #{"AC" "GC" "GA"} (stringify (lcs "AGCAT" "GAC"))))))

(deftest rosetta-test
  (testing "https://rosettacode.org/wiki/Longest_common_subsequence"
    (is (= #{[1 2 3 4]} (lcs [1 2 2 4 5 3 3 3 2 4] [1 2 3 4])))
    (is (= #{"tsitest"} (stringify (lcs "thisisatest" "testing123test"))))))

(deftest clrs
  (testing "Section 15.4"
    (is (= #{"GTCGTCGGAAGCCGGCCGAA"}
           (stringify (lcs "ACCGGTCGAGTGCGCGGAAGCCGGCCGAA" "GTCGTTCGGAATGCCGTTGCTCTGTAAA"))))
    (is (= #{"BCBA" "BDAB" "BCAB"} (stringify (lcs "ABCBDAB" "BDCABA"))))))
